package com.wutianyi.module.commons.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.wutianyi.utils.Utils;

/**
 * @author hanjiewu
 * 
 */
public class ConfigDO implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1871659292950902447L;
	
	private transient int id;
	private int pId;
	private ConfigTypeEnum type;
	private String name;
	private String value;
	private String description;
	private Date gmtCreate;
	private Date gmtModified;
	private transient List<ConfigDO> children;
	
	public ConfigDO() {
		type = ConfigTypeEnum.COMPANY_STANDARD_PROPERTIES;
		gmtCreate = new Date();
		gmtModified = new Date();
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public int getId() {
		return id;
	}
	
	public int getpId() {
		return pId;
	}
	
	public void setpId(int pId) {
		this.pId = pId;
	}
	
	public ConfigTypeEnum getType() {
		return type;
	}
	
	public void setType(ConfigTypeEnum type) {
		this.type = type;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getValue() {
		return value;
	}
	
	public void setValue(String value) {
		this.value = value;
	}
	
	public Date getGmtCreate() {
		return gmtCreate;
	}
	
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}
	
	public Date getGmtModified() {
		return gmtModified;
	}
	
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}
	
	public List<ConfigDO> getChildren() {
		return children;
	}
	
	public void setChildren(List<ConfigDO> children) {
		this.children = children;
	}
	
	public void addChild(ConfigDO config) {
		if (null == config) {
			return;
		}
		if (null == children) {
			children = new ArrayList<ConfigDO>();
		}
		children.add(config);
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[");
		builder.append("\n\tid: ");
		builder.append(id);
		builder.append("\n\tpId: ");
		builder.append(pId);
		builder.append("\n\ttype: ");
		builder.append(type);
		builder.append("\n\tname: ");
		builder.append(name);
		builder.append("\n\tvalue: ");
		builder.append(value);
		builder.append("\n\tgmtCreate: ");
		builder.append(gmtCreate);
		builder.append("\n\tgmtModified: ");
		builder.append(gmtModified);
		if (null != children && 0 != children.size()) {
			builder.append("\n\tchildren:[");
			for (ConfigDO config : children) {
				builder.append("\t{\n\t\tid: ");
				builder.append(config.id);
				builder.append("\n\t\tpId: ");
				builder.append(config.pId);
				builder.append("\n\t\ttype: ");
				builder.append(config.type);
				builder.append("\n\t\tname: ");
				builder.append(config.name);
				builder.append("\n\t\tvalue: ");
				builder.append(config.value);
				builder.append("\n\t\tgmtCreate: ");
				builder.append(config.gmtCreate);
				builder.append("\n\t\tgmtModified: ");
				builder.append(config.gmtModified);
				builder.append("\t}\n");
			}
		}
		builder.append("\n]");
		return builder.toString();
	}
	
	@Override
	public int hashCode() {
		HashFunction hf = Hashing.md5();
		HashCode hc = hf.newHasher().putInt(pId).putInt(type.getType())
				.putString(Utils.stringToString(name)).putString(Utils.stringToString(value))
				.putString(Utils.stringToString(description)).hash();
		return hc.asInt();
	}
	
	public long hash() {
		HashFunction hf = Hashing.md5();
		HashCode hc = hf.newHasher().putInt(pId).putInt(type.getType())
				.putString(Utils.stringToString(name)).putString(Utils.stringToString(value))
				.putString(Utils.stringToString(description)).hash();
		return hc.asLong();
	}
}
